// uart_transport.h
#ifndef UART_TRANSPORT_H
#define UART_TRANSPORT_H

#include <vector>
#include <cstdint>

const uint8_t FRAME_START_CODE = 0x56;
const uint8_t FRAME_END_CODE = 0x9E;
const uint8_t ESCAPE_SEQ_CODE = 0xCC;
const uint8_t FRAME_START_ESC_SEQ[] = {ESCAPE_SEQ_CODE, static_cast<uint8_t>(~FRAME_START_CODE & 0xff)};
const uint8_t FRAME_END_ESC_SEQ[] = {ESCAPE_SEQ_CODE, static_cast<uint8_t>(~FRAME_END_CODE & 0xff)};
const uint8_t ESCAPE_SEQ_ESC_SEQ[] = {ESCAPE_SEQ_CODE, static_cast<uint8_t>(~ESCAPE_SEQ_CODE & 0xff)};

class Frame
{
public:
    explicit Frame(const std::vector<uint8_t> &packet);
    std::vector<uint8_t> to_bytes();

private:
    std::vector<uint8_t> encode_payload(const std::vector<uint8_t> &data);
    static unsigned short calculate_checksum(const std::vector<uint8_t> &data);

    std::vector<uint8_t> packet;
};

class Uart_Transport
{
private:
    int uart_port;
    int baud_rate;
    int timeout;

public:
    Uart_Transport(int uart_port, int baud_rate, int timeout = 1000);
    Uart_Transport();
    std::vector<uint8_t> read(int timeout = 1000);
    void write(const std::vector<uint8_t> &data);
};

#endif // UART_TRANSPORT_H
